<template>
  <a-modal title="同步" :width="600" :visible="visible" @ok="handleModalConfirm" @cancel="handleModalCancel"
    :confirm-loading="modalLoading">
    <a-form-model ref="form" :model="form" :rules="formRules" :label-col="{ span: 4 }" :wrapper-col="{ span: 20 }">
      <a-form-model-item label="产品" prop="loanId">
        <a-select v-model="form.loanId" :options="loadOpt" placeholder="请选择" allowClear></a-select>
      </a-form-model-item>
      <a-form-model-item label="渠道" prop="channelIdList">
        <a-select v-model="form.channelIdList" mode="multiple" :maxTagCount="1" :options="channelOpt" placeholder="请选择"
          allowClear></a-select>
      </a-form-model-item>
    </a-form-model>
  </a-modal>
</template>

<script>
import * as Ajax from '@/api/loan';
export default {
  props: {
    visible: {
      type: Boolean,
    },
    default: {
      type: Object,
      default: () => { },
    },
  },
  data() {
    return {
      modalLoading: false,
      form: {
        loanId: undefined,
        channelIdList: [],
      },
      formRules: {
        loanId: { required: true, message: '请选择产品', trigger: ['blur', 'change'] },
        channelIdList: { required: true, message: '请选择渠道', trigger: ['blur', 'change'] },
      },
      loadOpt: [],
      channelOpt: [],
    };
  },
  watch: {
    visible(val) {
      if (val) {
        this.form = JSON.parse(JSON.stringify(this.default || '{}'));
      } else {
        // this.handleModalCancel();
      }
    },
  },
  mounted() {
    // this.getSelect();
  },
  methods: {
    getSelect() {
      Ajax.getFilterSelect().then(({ success, data }) => {
        if (success) {
          this.loadOpt = Array.isArray(data) ? data.map((it) => ({
            ...it,
            label: it.productNike,
            value: it.id,
          })) : [];
        };
      });
      Ajax.getChannelSelect().then(({ success, data }) => {
        if (success) {
          this.channelOpt = Array.isArray(data) ? data.map((it) => ({
            ...it,
            label: it.channelName,
            value: it.id,
          })) : [];
        };
      });
    },
    handleModalConfirm() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.modalLoading = true;
          const params = { ...this.form };
          params.loanName = this.loadOpt.find((it) => it.value === params.loanId).label;
          Ajax.synchronizeFilterList(params)
            .then(({ success }) => {
              if (success) {
                this.$message.success('同步成功');
                this.$emit('success');
                this.handleModalCancel();
              }
            })
            .finally(() => {
              this.modalLoading = false;
            });
        }
      });
    },
    handleModalCancel() {
      this.form = this.$options.data().form;
      this.$refs.form.resetFields();
      this.$emit('close');
    },
  },
};
</script>

<style lang="scss" scoped>
.table-wrap {
  margin-top: 20px;
  padding-top: 16px;
}

.btn-gruop {
  display: flex;
  justify-content: flex-end;
}

::v-deep .ant-form-item {
  &:last-child {
    margin-bottom: 0;
  }
}
</style>
